import { UserDao } from "../../account_server/dao/UserDao";
import { UserModel } from "../../account_server/model/UserModel";
import { ClubDao } from "../../hall_server/dao/ClubDao";
import { RoomDao } from "../../hall_server/dao/RoomDao";
import { RoomModel } from "../../hall_server/model/RoomModel";
import { RoomPlayer } from "../../hall_server/model/RoomPlayer";
import { LogServerHelper } from "../../log_server/LogServerHelper";
import { Config } from "../config/Config";
import { ErrorCode } from "../config/ErrorCode";
import { GameConfig } from "../config/GameConfig";
import { RoomConfig } from "../config/RoomConfig";
import { SocketConnection } from "../engine/socketserver/socket/SocketConnection";
import { SocketConnectionManager } from "../engine/socketserver/socket/SocketConnectionManager";
import { Logger } from "../engine/utils/Logger";
import { JhaoProto } from "../lib/net/protobuf/jhaoproto";
import { ResponseModel } from "../model/ResponseModel";
import { SystemSettingModel } from "../model/SystemSettingModel";
import { GameManager } from "./GameManager";
import { ServerManager } from "./ServerManager";

export class RoomManager {

    public static instance: RoomManager = new RoomManager();


    public async delete_room(deleteRoomIdList: Array<number>, deleteRoomList: Array<JhaoProto.IRoomModel> = [], msg: Array<string>, saveVideo: boolean = true) {
        if (deleteRoomIdList.length <= 0) {
            return false
        }
        // let dbSuc: boolean = await RoomDao.delete_room(deleteRoomIdList)
        RoomDao.delete_room(deleteRoomIdList)
        let dbSuc = true
        if (dbSuc) {
            for (let i = 0; i < deleteRoomList.length; i++) {
                let deleteRoom: JhaoProto.IRoomModel = deleteRoomList[i];
                if (deleteRoom.numOfTurns <= 1) {
                    // RoomManager.instance.checkAddRoomCost(deleteRoom)
                }
                if (deleteRoom.numOfTurns > 0) {
                    if (deleteRoom.roomType == RoomConfig.Room_Type.Create || deleteRoom.roomType == RoomConfig.Room_Type.Ziyou) {
                        // await GameManager.instance.notify_game_result(deleteRoom)
                        // if (saveVideo) {
                        //     if (deleteRoom.gameType == GameConfig.Game_Type.huanghua_mj || deleteRoom.gameType == GameConfig.Game_Type.ddz
                        //         || deleteRoom.gameType == GameConfig.Game_Type.cangxian_mj || deleteRoom.gameType == GameConfig.Game_Type.dazhonger_mj
                        //         || deleteRoom.gameType == GameConfig.Game_Type.jianziding || deleteRoom.gameType == GameConfig.Game_Type.paodekuai) {
                        //         let video: VideoInfo = deleteRoom.gameResult.videoList[deleteRoom.num_of_turns - 1] as VideoInfo;
                        //         if (video) {
                        //             video.time = DateUtil.now()
                        //             video.ju = deleteRoom.num_of_turns
                        //             if (deleteRoom.gameType == GameConfig.Game_Type.huanghua_mj) {
                        //                 let gameResult: huanghua_mj_GameResult = new huanghua_mj_GameResult();
                        //                 for (let j = 0; j < deleteRoom.playerList.length; j++) {
                        //                     gameResult.scoreList[j] = 0
                        //                 }
                        //                 video.videoResult = gameResult
                        //             }
                        //             else if (deleteRoom.gameType == GameConfig.Game_Type.ddz) {
                        //                 let gameResult: DdzGameResult = new DdzGameResult();
                        //                 for (let j = 0; j < deleteRoom.playerList.length; j++) {
                        //                     gameResult.scoreList[j] = 0
                        //                 }
                        //                 video.videoResult = gameResult
                        //             }
                        //             else if (deleteRoom.gameType == GameConfig.Game_Type.jianziding) {
                        //                 let gameResult: jianziding_GameResult = new jianziding_GameResult();
                        //                 for (let j = 0; j < deleteRoom.playerList.length; j++) {
                        //                     gameResult.scoreList[j] = 0
                        //                 }
                        //                 video.videoResult = gameResult
                        //             }
                        //             else if (deleteRoom.gameType == GameConfig.Game_Type.paodekuai) {
                        //                 let gameResult: paodekuai_GameResult = new paodekuai_GameResult();
                        //                 for (let j = 0; j < deleteRoom.playerList.length; j++) {
                        //                     gameResult.scoreList[j] = 0
                        //                 }
                        //                 video.videoResult = gameResult
                        //             }
                        //             else if (deleteRoom.gameType == GameConfig.Game_Type.dazhonger_mj) {
                        //                 let gameResult: dazhonger_mj_GameResult = new dazhonger_mj_GameResult();
                        //                 for (let j = 0; j < deleteRoom.playerList.length; j++) {
                        //                     gameResult.scoreList[j] = 0
                        //                 }
                        //                 video.videoResult = gameResult
                        //             }
                        //             else if (deleteRoom.gameType == GameConfig.Game_Type.cangxian_mj) {
                        //                 let gameResult: cangxian_mj_GameResult = new cangxian_mj_GameResult();
                        //                 for (let j = 0; j < deleteRoom.playerList.length; j++) {
                        //                     gameResult.scoreList[j] = 0
                        //                 }
                        //                 video.videoResult = gameResult
                        //             }
                        //             await GameManager.instance.add_game_result(deleteRoom)
                        //             video.gameResultId = deleteRoom.gameResult.id;
                        //         }
                        //         // Logger.log("删除房间==========", deleteRoom.gameResult.id, video.gameResultId)
                        //         if (deleteRoom.roomType == RoomConfig.Room_Type.Create) {
                        //             await RoomVideoDao.insertVideo(video)
                        //         }
                        //     }
                        // }
                    }
                }
                // RoomManager.instance.notifyRoomRelease(deleteRoom, msg[i])
            }
        }
        return dbSuc
    }

    public async leave_room(room_id: number, userid: number) {
        let room: JhaoProto.IRoomModel = RoomModel.roomMap.get(room_id)
        let respModel: ResponseModel = new ResponseModel()
        if (room) {
            let canLeave: boolean = true
            if (room.numOfTurns > 0) {
                canLeave = false
                respModel.code = ErrorCode.Code.Fail
                respModel.msg = "游戏已经开始不能离开";
            } else {
                if (userid == room.creator) {
                    if (room.roomConfigBase.costType == RoomConfig.cost_type.fangzhu) { //房主支付的房间要所有人都跑了才能走
                        // if (room.getPlayerNum() > 1) {
                        //     canLeave = false;
                        //     respModel.code = ErrorCode.Code.Fail
                        //     respModel.msg = "房主支付的房间要其他人都走了才能解散";
                        // }
                    }
                }
            }
            if (canLeave) {
                respModel.code = ErrorCode.Code.Suc
                // room.leavePlayer(userid)
                // await RoomDao.update_room(room)
                // await UserDao.setRoom_id(userid, 0)
            }
        } else {
            respModel.code = ErrorCode.Code.Fail
            respModel.msg = "不在房间里面";
        }
        if (respModel.code == ErrorCode.Code.Suc) {
            // ServerManager.instance.leaveRoom(room)
            // RoomManager.instance.notify_player_leave(room, userid)
        }
        return respModel
    }


    private notifyRoomRelease(room: RoomModel, msg: string = "时间到，该房间由系统释放") {
        ServerManager.instance.deleteRoom(room.room_id)
        for (let i = 0; i < room.playerList.length; i++) {
            let player: RoomPlayer = room.playerList[i]
            if (player) {
                let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                if (conn) {
                    let respModel: ResponseModel = new ResponseModel()
                    respModel.code = ErrorCode.Code.Suc
                    respModel.msg = msg
                    // conn.createMsgToClient(0, ProtoConfig.notify_room_release, respModel)
                }
            }
        }
    }

    public notify_player_update(room: RoomModel) {
        if (Config.serverType == Config.ServerType_List.hall_server) {
            return;
        }
        for (let i = 0; i < room.playerList.length; i++) {
            let player: RoomPlayer = room.playerList[i]
            if (player) {
                let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                if (conn) {
                    let respModel: ResponseModel = new ResponseModel()
                    respModel.code = ErrorCode.Code.Suc
                    respModel.data = room.playerList
                    // conn.createMsgToClient(0, ProtoConfig.notify_player_update, respModel)
                }
            }
        }
    }

    public notify_player_join(room: RoomModel) {
        if (Config.serverType == Config.ServerType_List.hall_server) {
            return;
        }
        for (let i = 0; i < room.playerList.length; i++) {
            let player: RoomPlayer = room.playerList[i]
            if (player) {
                let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                if (conn) {
                    let respModel: ResponseModel = new ResponseModel()
                    respModel.code = ErrorCode.Code.Suc
                    respModel.data = room.playerList
                    // conn.createMsgToClient(0, ProtoConfig.notify_player_join, respModel)
                }
            }
        }
    }

    public async notify_player_leave(room: RoomModel, leave_userid: number) {
        if (Config.serverType == Config.ServerType_List.hall_server) {
            return;
        }
        let respModel: ResponseModel = new ResponseModel()
        respModel.code = ErrorCode.Code.Suc
        let respData = {
            "leave_userid": leave_userid,
            "playerList": room.playerList
        }
        respModel.data = respData
        let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(leave_userid)
        if (conn) {
            // conn.createMsgToClient(0, ProtoConfig.notify_player_leave, respModel)
        }
        for (let i = 0; i < room.playerList.length; i++) {
            let player: RoomPlayer = room.playerList[i]
            if (player) {
                let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                if (conn) {
                    // conn.createMsgToClient(0, ProtoConfig.notify_player_leave, respModel)
                }
            }
        }
        // if (room.getPlayerNum() == 0 && room.roomType != RoomConfig.Room_Type.Ziyou) {
        if (RoomModel.getPlayerNum(room) == 0) {
            Logger.log("notify_player_leave===所有人都离开房间了回收房间=", room)
            let dbSuc: boolean = await RoomManager.instance.delete_room([room.room_id], [room], ["所有人都离开房间了回收房间"])
            if (dbSuc) {

            } else {
                respModel.code = ErrorCode.Code.Fail
                respModel.msg = "清除房间信息失败";
            }
        }
    }

    public async notify_game_start(room: RoomModel) {
        if (Config.serverType == Config.ServerType_List.hall_server) {
            return;
        }
        room.lastCheckAutoReadyTIme = 0
        // if (Config.serverType == Config.ServerType_List.ddz_server) {
        //     ddz_Gamemanager.instance.notify_game_start(room, true)
        // }
        // else if (Config.serverType == Config.ServerType_List.paodekuai_server) {
        //     paodekuai_Gamemanager.instance.notify_game_start(room, true)
        // }
        // else if (Config.serverType == Config.ServerType_List.jianziding_server) {
        //     jianziding_Gamemanager.instance.notify_game_start(room, true)
        // }
        // else if (Config.serverType == Config.ServerType_List.huanghua_mj_server) {
        //     huanghua_mj_Gamemanager.instance.notify_game_start(room, true)
        // }
        // else if (Config.serverType == Config.ServerType_List.cangxian_mj_server) {
        //     cangxian_mj_Gamemanager.instance.notify_game_start(room, true)
        // }
        // else if (Config.serverType == Config.ServerType_List.dazhonger_mj_server) {
        //     dazhonger_mj_Gamemanager.instance.notify_game_start(room, true)
        // }
        //通知其他服务器增加回合
        ServerManager.instance.notify_game_start(room)
    }

    public async checkDecRoomCost(room: RoomModel) {
        await this.decRoomCreatorCost(room)
        await this.decRoompingjunCost(room)
        await this.decRoomJinbi(room)
    }

    public async decRoomMatchJinbi(room: RoomModel) {
        if (room.roomType == RoomConfig.Room_Type.Match) {
            if (room.conf["cost_type"] == RoomConfig.cost_type.enterMinJinbi) {
                let decNum: number = room.conf["cost"]
                for (let i = 0; i < room.playerList.length; i++) {
                    let player: RoomPlayer = room.playerList[i];
                    if (player && player.matchTurn == 1) {
                        let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                        // if (conn && conn.isLogin() && conn.user) {
                        //     conn.user.coins -= decNum
                        //     conn.user.coins = await UserDao.decCoins(player.userid, decNum);
                        //     LogServerHelper.log_add_user_coins_history(conn.user.userid, -decNum, conn.user.coins, GameConfig.Coins_History_Reason.match_create, "比赛场服务费，扣除了" + decNum + "金豆")
                        // }
                    }
                }
            }
        }
    }

    //房主支付 扣钱
    private async decRoomCreatorCost(room: RoomModel) {
        if (room.conf["cost_type"] == RoomConfig.cost_type.fangzhu && room.num_of_turns == 1) {
            let player: RoomPlayer = room.getPlayerByUserid(room.creator)
            if (player) {
                let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                if (conn && conn.isLogin() && conn.user) {
                    // conn.user.money -= room.conf["cost"]
                    // conn.user.money = await UserDao.decGems(player.userid, room.conf["cost"]);
                    // LogServerHelper.log_add_user_spend_daily(player.userid, "gems", room.conf["cost"])
                    // LogServerHelper.log_add_user_gems_history(player.userid, -room.conf["cost"], conn.user.money, GameConfig.Gems_History_Reason.Create_Room, GameConfig.GameNameList[room.gameType] + "房主支付，创建房间扣除钻石" + room.conf["cost"])
                }

            } else {
                Logger.warn("房主支付发现不存在房主:", room)
            }
        }
    }

    public checkAddRoomCost(room: RoomModel) {
        // this.addRoomCreatorCost(room)
        // this.addRoompingjunCost(room)
        this.addRoomClubOwnerCost(room)
    }

    //房主支付 退钱
    private async addRoomCreatorCost(room: RoomModel) {
        if (room.conf["cost_type"] == RoomConfig.cost_type.fangzhu && room.num_of_turns == 1) {
            let player: RoomPlayer = room.getPlayerByUserid(room.creator)
            if (player) {
                let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                // if (conn && conn.isLogin() && conn.user) {
                //     conn.user.money += room.conf["cost"]
                //     conn.user.money = await UserDao.addGems(player.userid, room.conf["cost"]);
                //     LogServerHelper.log_add_user_gems_history(player.userid, room.conf["cost"], conn.user.money, GameConfig.Gems_History_Reason.Dismiss_Room, GameConfig.GameNameList[room.gameType] + "房主支付，解散房间退回钻石" + room.conf["cost"])
                // }

            } else {
                Logger.warn("房主支付发现不存在房主:", room)
            }
        }
    }

    //平摊房费
    private async decRoompingjunCost(room: RoomModel) {
        if (room.conf["cost_type"] == RoomConfig.cost_type.pingjun && room.num_of_turns == 1) {
            let decNum: number = Math.floor(room.conf["cost"] / room.conf["renshu"])
            for (let i = 0; i < room.playerList.length; i++) {
                let player: RoomPlayer = room.playerList[i];
                if (player) {
                    // let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                    // if (conn && conn.isLogin() && conn.user) {
                    //     let user: UserModel = conn.user;
                    //     user.money -= decNum
                    //     user.money = await UserDao.decGems(player.userid, decNum);
                    //     LogServerHelper.log_add_user_gems_history(player.userid, -decNum, user.money, GameConfig.Gems_History_Reason.Create_Room, GameConfig.GameNameList[room.gameType] + "平摊房费，创建房间扣除钻石" + decNum)
                    //     LogServerHelper.log_add_user_spend_daily(player.userid, "gems", decNum)
                    // }

                }
            }
        }
    }

    //平摊房费 退钱
    private async addRoompingjunCost(room: RoomModel) {
        if (room.conf["cost_type"] == RoomConfig.cost_type.pingjun && room.num_of_turns == 1) {
            let decNum: number = Math.floor(room.conf["cost"] / room.conf["renshu"])
            for (let i = 0; i < room.playerList.length; i++) {
                // let player: RoomPlayer = room.playerList[i];
                // if (player) {
                //     let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                //     if (conn && conn.isLogin() && conn.user) {
                //         conn.user.money += decNum
                //         conn.user.money = await UserDao.addGems(player.userid, decNum);
                //         LogServerHelper.log_add_user_gems_history(player.userid, decNum, conn.user.money, GameConfig.Gems_History_Reason.Dismiss_Room, GameConfig.GameNameList[room.gameType] + "平摊房费，解散房间退回钻石" + decNum)
                //     }
                // }
            }
        }
    }

    //大赢家付款
    public async decRoomDayingjiaCost(room: RoomModel, bigWinUserid: number) {
        if (room.conf["cost_type"] == RoomConfig.cost_type.dayingjia && room.num_of_turns == 1) {
            let player: RoomPlayer = room.getPlayerByUserid(bigWinUserid)
            if (player) {
                // let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                // if (conn && conn.isLogin() && conn.user) {
                //     conn.user.money -= room.conf["cost"]
                //     conn.user.money = await UserDao.decGems(player.userid, room.conf["cost"]);
                //     LogServerHelper.log_add_user_gems_history(player.userid, -room.conf["cost"], conn.user.money, GameConfig.Gems_History_Reason.Create_Room, GameConfig.GameNameList[room.gameType] + "大赢家付款，创建房间扣除钻石" + room.conf["cost"])
                //     LogServerHelper.log_add_user_spend_daily(player.userid, "gems", room.conf["cost"])
                // }
            } else {
                Logger.warn("大赢家支付发现不存在大赢家:", room)
            }
        }
    }

    //圈主支付
    public async decRoomClubOwnerCost(userid: number, cost: number, gameType: number) {
        let left_gems: number = await UserDao.decGems(userid, cost)
        ServerManager.instance.notify_update_gems(userid)
        LogServerHelper.log_add_user_gems_history(userid, -cost, left_gems, GameConfig.Gems_History_Reason.Create_Room, GameConfig.GameNameList[gameType] + "圈主支付，创建房间扣除钻石" + cost)
        LogServerHelper.log_add_user_spend_daily(userid, "gems", cost)
        return true
    }

    //圈主支付退钱
    private async addRoomClubOwnerCost(room: RoomModel) {
        if (room.conf["cost_type"] == RoomConfig.cost_type.quanzhu && room.num_of_turns <= 1 && room.clubid > 0) {
            // let clubObj: object = await ClubDao.getClub(room.clubid);
            // if (clubObj) {
                // let creator: number = clubObj["creator"]
                // let clubOwnerConn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(creator)
                // if (clubOwnerConn && clubOwnerConn.isLogin() && clubOwnerConn.user) {
                //     clubOwnerConn.user.money += room.conf["cost"]
                // }
                // let left_gems: number = await UserDao.addGems(creator, room.conf["cost"]);
                // ServerManager.instance.notify_update_gems(creator)
                // LogServerHelper.log_add_user_gems_history(creator, room.conf["cost"], left_gems, GameConfig.Gems_History_Reason.Dismiss_Room, GameConfig.GameNameList[room.gameType] + "圈主支付，解散房间退回钻石" + room.conf["cost"])
            // }
        }
    }

    public async decRoomJinbi(room: RoomModel) {
        if (room.roomType == RoomConfig.Room_Type.Jinbi) {
            if (room.conf["cost_type"] == RoomConfig.cost_type.enterMinJinbi) {
                let decNum: number = room.conf["cost"]
                for (let i = 0; i < room.playerList.length; i++) {
                    let player: RoomPlayer = room.playerList[i];
                    if (player) {
                        // let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                        // if (conn && conn.isLogin() && conn.user) {
                        //     conn.user.coins -= decNum
                        //     conn.user.coins = await UserDao.decCoins(player.userid, decNum);
                        //     LogServerHelper.log_add_user_coins_history(conn.user.userid, -decNum, conn.user.coins, GameConfig.Coins_History_Reason.JinbiChang_create, "金豆场金豆房，扣除了" + decNum + "金豆")
                        //     player.score = conn.user.coins
                          
                        // }
                    }
                }
            }
        }
        else if (room.roomType == RoomConfig.Room_Type.Jinbi_Special) {
            if (room.conf["cost_type"] == RoomConfig.cost_type.enterMinJinbi) {
                let decNum: number = room.conf["cost"]
                for (let i = 0; i < room.playerList.length; i++) {
                    let player: RoomPlayer = room.playerList[i];
                    if (player) {
                        // let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                        // if (conn && conn.isLogin() && conn.user) {
                        //     conn.user.coins -= decNum
                        //     conn.user.coins = await UserDao.decCoins(player.userid, decNum);
                        //     LogServerHelper.log_add_user_coins_history(conn.user.userid, -decNum, conn.user.coins, GameConfig.Coins_History_Reason.Jinbi_Special_create, "金豆场特殊房，扣除了" + decNum + "金豆")
                        //     player.score = conn.user.coins
                        // }
                    }
                }
            }
        }
        else if (room.roomType == RoomConfig.Room_Type.Ziyou) {
            if (room.conf["cost_type"] == RoomConfig.cost_type.enterMinJinbi && room.num_of_turns == 1) {
                let decNum: number = room.conf["cost"]
                for (let i = 0; i < room.playerList.length; i++) {
                    let player: RoomPlayer = room.playerList[i];
                    if (player) {
                        // let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                        // if (conn && conn.isLogin() && conn.user) {
                        //     conn.user.coins -= decNum
                        //     conn.user.coins = await UserDao.decCoins(player.userid, decNum);
                        //     LogServerHelper.log_add_user_coins_history(conn.user.userid, -decNum, conn.user.coins, GameConfig.Coins_History_Reason.Ziyouchang_create, "自由场房，扣除了" + decNum + "金豆")
                        //     player.score = conn.user.coins
                        // }
                    }
                }
            }
        }
        else if (room.roomType == RoomConfig.Room_Type.Match) {
            // if (room.conf["cost_type"] == RoomConfig.cost_type.enterMinJinbi) {
            //     let decNum: number = room.conf["cost"]
            //     for (let i = 0; i < room.playerList.length; i++) {
            //         let player: RoomPlayer = room.playerList[i];
            //         if (player) {
            //             let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
            //             if (conn && conn.isLogin() && conn.user) {
            //                 conn.user.coins -= decNum
            //                 conn.user.coins = await UserDao.decCoins(player.userid, decNum);
            //                 LogServerHelper.log_add_user_coins_history(conn.user.userid, -decNum, conn.user.coins, GameConfig.Coins_History_Reason.match_create, "比赛场服务费，扣除了" + decNum + "金豆")

            //             }
            //         }
            //     }
            // }
        }
    }

    public async checkUpdatePlayerCoin(user: UserModel) {
        // let room_id: number = user.room_id
        // let room: RoomModel = RoomModel.roomMap.get(room_id)
        // if (RoomModel.roomMap.has(room_id)) {
        //     if (room.roomType == RoomConfig.Room_Type.Ziyou) {
        //         for (let i = 0; i < room.playerList.length; i++) {
        //             if (room.playerList[i] && room.playerList[i].userid == user.userid) {
        //                 room.playerList[i].score = user.coins;
        //                 RoomManager.instance.notify_player_update(room)
        //                 break;
        //             }
        //         }
        //     }
        // }
    }


    private async sendDailiCoins(userid: number, coins: number, dailiLevel: number = 1) {
        // let percentArr: Array<number> = [60, 10, 5, 5]
        if (dailiLevel < 3) {//最多有三级代理
            let choushui: string = SystemSettingModel.instance.ziyouchang_ticheng
            let percentArr: Array<string> = choushui.split(",")
            let sendPercent: number = parseInt(percentArr[dailiLevel - 1]);
            let userData: object = await UserDao.get_user_data_by_userid(userid)
            let income = userData["income"]
            let qudaodaili = userData["qudaodaili"]
            if (qudaodaili == 1) {
                sendPercent += parseInt(percentArr[3]);
            }
            sendPercent = sendPercent / 100;
            let nowIncome: number = income + coins * sendPercent
            await UserDao.update_income(userid, nowIncome)
            let invite = userData["invite"]
            if (invite && invite > 0) {
                dailiLevel++
                this.sendDailiCoins(invite, coins, dailiLevel)
            }
        }
    }
}